# -*- coding: utf-8 -*-

import sys
import os
import simplejson as json
from ParentalController import *
from NetworkClientSupport import *

from Signal import *
from IMSignals import *

class KiduxParentalController(ParentalController, NetworkClientSupport):
    
    def __init__(self, plugin, account, paDebug = False):
        NetworkClientSupport.__init__(self, "im")
        self.account = account
        self.plugin = plugin
        self.paDebug = paDebug

    def parentalControllerDebug(self, msg):
        if self.paDebug:
            print "****************************************"
            print self.plugin.debugCount
            print "[ParentallControllerDEBUG]:\n", msg
            self.plugin.debugCount += 1
            print "****************************************"

    def start(self):
        self.open()

    def stop(self):
        import time
        time.sleep(0.5)
        self.close()

    def process(self, object):
        print object , object.id
        if self.ignoreSignal(object):
            return

        elif isinstance(object, SetConversation):
            self.plugin.setConversation(object)
        else:
            #TODO: debug stuff
            print "PROCESS: "
            print object , object.id
            raise NotImplementedError

    def ignoreSignal(self, object):
        if object == None:
            return True

        if object.applicationId != "im":
            return True

        if isinstance(object, Ack):
            return True
        
        if isinstance(object, Shutdown):
            return True
        
        return False

    def createApplicationSignal(self, className):
        if className == SetConversation.javaClassName():
            return SetConversation(None, None)
        elif className == AccountSignal.javaClassName():
            return AccountSignal(None, None)
        else:
            raise NotImplementedError


    def isContactAllowed(self, contactId):
        self.parentalControllerDebug("isContactAllowed")
        signal = self.query(IsContactAllowed(self.account, contactId))
        return signal.bool

    def isFileTransferAllowed(self, contactId):
        self.parentalControllerDebug("isFileTransferAllowed")
        signal = self.query(IsFileTransferAllowed(self.account, contactId))
        return signal.bool

    def isAccountAllowed(self, accountId):
        self.parentalControllerDebug("isAccountAllowed")
        signal = self.query(FindAccount(accountId))
        return signal


    def sigTests(self, accountId, buddies):
        self.parentalControllerDebug("sigTests")
        signal = self.send(SigTests(accountId, buddies))

    def setAvatar(self, accountId, contactId,  imagePath):
        self.parentalControllerDebug("setAvatar")
        self.send(SetAvatar(self.accountId, contactId,  imagePath))

    def setContactRemove(self, accountId, contactId):
        self.parentalControllerDebug("setContactRemove")
        self.send(SetContactRemove(self.accountId, contactId))

    def logMessage(self, accountId, accountIP, contactId,  contactIP, message, isSender):
        self.parentalControllerDebug("logMessage")
        signal = self.send(LogMessage(accountId, accountIP, contactId, contactIP, message, isSender))

    def addContact(self, accountId, contactId, group):
        self.parentalControllerDebug("addContact")
        self.send(AddContact(accountId, contactId, group))

    def setNickName(self, accountId, contactId, nickName):
        self.parentalControllerDebug("setNickName")
        self.send(SetNickName(accountId, contactId, nickName))

    def setPersonalMessage(self, accountId, contactId, personalMessage):
        self.parentalControllerDebug("setPersonalMessage")
        self.send(SetPersonalMessage(accountId, contactId, personalMessage))

    def setGroup(self, accountId, contactId, group):
        self.parentalControllerDebug("setGroup")
        self.send(SetGroup(accountId, contactId, group))

    def setAvatar(self, accountId,  contactId, avatarPath):
        self.parentalControllerDebug("setAvatar")
        self.send(SetAvatar(accountId,  contactId, avatarPath))

    def newAccount(self, accountId, buddies):
        self.parentalControllerDebug("newAccount")
        self.send(NewAccount(accountId, buddies))
    
    def setStatus(self, accountId, contactId, contactStatus):
        self.parentalControllerDebug("setStatus")
        self.send(SetStatus(accountId, contactId, contactStatus))

    def setBuddyListStatus(self, accountId, onlineContacts):
        self.parentalControllerDebug("setBuddyListStatus")
        self.send(SetBuddyListStatus(accountId, onlineContacts))

    def disconnect(self, accountId):
        self.parentalControllerDebug("disconnect")
        self.send(SetStatus(accountId, None, False))

    def startConversation(self, accountId, conversationId, contacts):
        self.parentalControllerDebug("startConversation")
        self.send(StartConversation(accountId, conversationId, contacts))

    def logConversation(self, accountId, conversationId, contactId, message):
        self.parentalControllerDebug("logConversation")
        self.send(LogConversation(accountId, conversationId, contactId, message))

    def enterConversation(self, accountId, conversationId, contactId):
        self.parentalControllerDebug("enterConversation")
        self.send(EnterConversation(accountId, conversationId, contactId))

    def leaveConversation(self, accountId, conversationId, contactId):
        self.parentalControllerDebug("leaveConversation")
        self.send(LeaveConversation(accountId, conversationId, contactId))

    def logFileTransfer(self, accountId, contactId, conversationId, path):
        self.parentalControllerDebug("logFileTransfer")
        self.send(LogFileTransfer(accountId, contactId, conversationId, path))

